<?php

namespace login\controller;

require_once('/login/model/Loginmodel.php');
require_once('/login/view/UserInfo.php');
require_once('/login/view/Loginview.php');
require_once('/login/view/HTMLView.php');
require_once('/login/controller/AddUserController.php');
require_once('/loggedin/view/MemberAreaView.php');
require_once("/common/view/View.php");

class userLogin {
	/**
	 * @var $usrInfoView UserInfo objekt.
	 */
	private $usrInfoView;
	/**
	 * @var $loginM Loginmodel objekt.
	 */
	private $loginM;
	/**
	 * @var $loginView Loginview objekt.
	 */
	private $loginView;
	/**
	 * @var $pageView HTMLView objekt.
	 */
	private $pageView;
	/**
	 * @var \login\controller\AddUserController
	 */
	private $addUserController;
	/**
	 * @var \loggedin\view\MemberAreaView
	 */
	private $memberAreaView;
	/**
	 * @var \common\view\View
	 */
	private $view;
	/**
	 * @var $arrayusername string.
	 */
	private static $arrayusername = "username";
	/**
	 * @var $arraypassword string.
	 */
	private static $arraypassword = "password";

	function __construct() {
		$this->usrInfoView = new \login\view\UserInfo();
		$this->loginM = new \login\model\Loginmodel();
		$this->loginView = new \login\view\Loginview();
		$this->pageView = new \login\view\HTMLView();
		$this->addUserController = new \login\controller\AddUserController();
		$this->memberAreaView = new \loggedin\view\MemberAreaView();
		$this->view = new \common\view\View();
	}

	/**
	 * @return string med html kod
	 */
	public function loginUser() {
		if ($this->loginView->getRegisterInfo()) {
			return $this->pageView->getRegisterPage();
		}
		else if ($this->loginView->getaddUserInfo()) {
			return $this->addUserController->AddUser();
		}
		else if ($this->loginView->getLoginInfo()) {
			try {
				$userInfo = $this->usrInfoView->getUserInfo();
				if($this->loginM->validateUsrInfo($userInfo->getUsername(),
												  $userInfo->getPassword())) {
					$this->loginView->setSessionLogin();
					$this->usrInfoView->setUserLoggedinCookie($userInfo->getUsername());
				}
				$this->view->relocate();
			}
			catch (\Exception $e) {
				$this->pageView->setLogginInfo($e->getMessage());
				$this->pageView->setInputValue();
			}
		}
		return $this->pageView->getLoginPage();
	}
}